#include "solver.h"

pwork *setup(params *p)
{
  idxint i = 0;
  double *ptr;
  static double b[42] = {0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.0, 0.0, 0.0, 0.0, 0, 0.0, 0.0, 0.0, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0, -0.5, -0.5, 0.0, 0.0, 0.0, 0.0, 0};
  b[4] = p->d;
  b[10] = p->e;
  b[20] = p->b1;
  b[27] = p->b2;
  b[34] = p->b3;
  b[41] = p->b4;
  static double c[50] = {1}; /* rest = {0.0}; */
  static double h[19]; /* = {0.0}; */
  static idxint q[6] = {2, 2, 3, 3, 3, 3};
  static idxint Gjc[51] = {0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19};
  static idxint Gir[19] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18};
  static double Gpr[19] = {-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0};
  static idxint Ajc[51] = {0, 1, 2, 3, 4, 5, 10, 11, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 32, 34, 36, 39, 41, 43, 45, 47, 50, 52, 55, 58, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93};
  static idxint Air[93] = {11, 5, 6, 7, 8, 2, 18, 25, 32, 39, 9, 3, 19, 26, 33, 40, 14, 15, 16, 21, 22, 23, 28, 29, 30, 35, 36, 37, 0, 1, 0, 4, 1, 2, 1, 3, 5, 6, 7, 5, 10, 6, 8, 7, 9, 11, 12, 11, 35, 36, 12, 13, 12, 28, 29, 13, 14, 15, 13, 21, 22, 16, 17, 16, 20, 17, 18, 17, 19, 23, 24, 23, 27, 24, 25, 24, 26, 30, 31, 30, 34, 31, 32, 31, 33, 37, 38, 37, 41, 38, 39, 38, 40};
  static double Apr[93] = {-1.0,1.0,-1.0,-1.0,1.0,0,0,0,0,0,1.0,0,0,0,0,0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,1.0,1.0,-1.0,1.0,-1.0,-1.0,-1.0,-1.0,1.0,-1.0,1.0,0.5,-0.5,1.0,-1.0,1.0,0.5,-0.5,1.0,0.5,-0.5,1.0,0.5,-0.5,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,-1.0};
  Apr[6] = p->a11;
  Apr[12] = p->a12;
  Apr[7] = p->a21;
  Apr[13] = p->a22;
  Apr[8] = p->a31;
  Apr[14] = p->a32;
  Apr[9] = p->a41;
  Apr[15] = p->a42;
  Apr[5] = p->c11;
  Apr[11] = p->c12;
  return ECOS_setup(50 /* num vars */, 19 /* num cone constraints */, 42 /* num eq constraints */, 3 /* num linear cones */, 6 /* num second-order cones */, q, Gpr, Gjc, Gir, Apr, Ajc, Air, c, h ,b);
}

int solve(pwork *w, vars *sol)
{
  idxint i = 0;
  int exitflag = ECOS_solve(w);
  sol->x1 = w->x[5];
  sol->x2 = w->x[7];
  return exitflag;
}

void cleanup(pwork *w)
{
  ECOS_cleanup(w,0);
}

